package user

import (
	"enjoy_wangyashuo/formdata"
	"enjoy_wangyashuo/internal/model/mysql"
	"gorm.io/gorm"
)

// Username 用户地址表
type UserAddress struct {
	gorm.Model
	UserId    int    `gorm:"type:int(10);comment:'用户ID"`
	Name      string `gorm:"type:varchar(20);comment:收货人地址"`
	Phone     string `gorm:"type:char(11);comment:收货人手机号"`
	Province  string `gorm:"type:varchar(10);comment:省份"`
	City      string `gorm:"type:varchar(10);comment:城市"`
	District  string `gorm:"type:varchar(10);comment:区"`
	Address   string `gorm:"type:varchar(255);comment:详细地址"`
	IsDefault int8   `gorm:"type:tinyint(2);comment:是否默认地址(0:否,1:是)"`
}

// 创建用户地址
func (u *UserAddress) CreateAddress() error {
	return mysql.WithMysql(func(db *gorm.DB) error {
		return db.Create(u).Error
	})
}

// 获取用户地址列表
func (u *UserAddress) GetAddrListByUserId() (addressList []formdata.AddrInfo, err error) {
	var list []*UserAddress
	return addressList, mysql.WithMysql(func(db *gorm.DB) error {
		err = db.Where("user_id =?", u.UserId).Find(&list).Error
		for _, val := range list {
			addressList = append(addressList, formdata.AddrInfo{
				ID:        int64(val.ID),
				UserID:    val.UserId,
				Name:      val.Name,
				Phone:     val.Phone,
				Province:  val.Province,
				City:      val.City,
				District:  val.District,
				Address:   val.Address,
				IsDefault: val.IsDefault,
			})
		}
		return nil
	})
}
